package com.etc.controller;

import com.etc.dao.MovieDao;
import com.etc.dao.Movie_cinemaDao;
import com.etc.entity.Cinema;
import com.etc.entity.Movie;
import com.etc.entity.Movie_cinema;
import com.etc.service.CinemaService;
import com.etc.service.Movie_cinema_relationService;
import com.etc.util.Msg;
import com.etc.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/5/21.
 */
@RestController
@RequestMapping("mcs")
public class Movie_cinema_relationController {
    @Autowired
    Movie_cinema_relationService mcs;
    @Autowired
    CinemaService cinemaService;
    @Autowired
    Movie_cinemaDao mcd;


    @PostMapping(value = "addcinema")
    @ResponseBody
    public Msg<String> addcinema(@RequestBody Movie_cinema movie_cinema) {
       Cinema cinema=new Cinema();
        Msg msg = new Msg();
        cinema.setMid(movie_cinema.getMid());
        cinema.setCname(movie_cinema.getCname());
        cinema.setMstartime(movie_cinema.getMstartime());
        cinema.setMprice(movie_cinema.getMprice());
        Cinema cinema1 = cinemaService.addcinema(cinema);
        if (cinema1 != null) {
            msg.setMsg("添加成功");
        } else {
            msg.setMsg("时间已经有排片");
        }
        return msg;
    }
    @PutMapping(value = "updatecinema")
    @ResponseBody
    public Msg<String> updatecinema(@RequestBody Movie_cinema movie_cinema) {
        Cinema cinema=new Cinema();
        Msg msg = new Msg();
        cinema.setMid(movie_cinema.getMid());
        cinema.setCid(movie_cinema.getCid());
        cinema.setCname(movie_cinema.getCname());
        cinema.setMstartime(movie_cinema.getMstartime());
        cinema.setMprice(movie_cinema.getMprice());
        Cinema cinema1 = cinemaService.updatecinema(cinema);
        if (cinema1 != null) {
            msg.setMsg("修改成功");
        } else {
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @GetMapping(value = "mcslist")
    @ResponseBody
    public PageData<Movie_cinema> getcinemas(@RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit, @RequestParam(value = "content", required = false) String content) throws ServletException, IOException {

        if (content == null) {
            content = "";
        }
        PageData<Movie_cinema> pd = mcs.getCinemaByPage(page, limit, content);
        pd.setCode(0);
        return pd;
    }
    @GetMapping(value = "getmc")
    @ResponseBody
    public Movie_cinema getcinemas(String cname){
        List<Movie_cinema> list=mcd.findByCname1(cname);
        if (list.size()>0){

            return list.get(list.size()-1);
        }else{
            Movie_cinema mc=new Movie_cinema();
            mc.setMstartime(new Date());
            return mc;

        }
    }

}
